<?php

abstract class AbstractCredentialParser
{
  /**
   * @var array
   */
  protected $options;

  public function __construct (array $options = array())
  {
    $this->options = $options;
  }

  public function getOption ($name, $default = '')
  {
    if (isset($this->options[$name]))
    {
      return $this->options[$name];
    }
    else
    {
      return $default;
    }
  }

  public function hasOption ($name)
  {
    return isset($this->options[$name]);
  }

  /**
   * @return sfActions
   */
  public function getActions ()
  {
    return $this->getOption('actions', null);
  }

  /**
   * @param string $credential
   * @return string Parsed credential
   */
  abstract public function parse ($credential);
  
  public function executeMethodChain($obj, $chain) {
    if(!is_array($chain)) {
      return $this->executeMethodChain($obj, explode('.', $chain));
    } else {
      $current_method = array_shift($chain);
      if(is_null($current_method)) {
        return $obj;
      } else {
        $obj = $obj->$current_method();
        return $this->executeMethodChain($obj, $chain);
      }
    }
  }
}